const connection = require("../app/database");

class UserService {
  // 创建用户
  async create(user) {
    // 1、获取用户
    const { name, password } = user;
    // 2、拼接statement
    const statement = "INSERT INTO `user` (name, password) VALUES (?, ?);";
    // 3、执行sql操作
    const [result] = await connection.execute(statement, [name, password]);
    return result;
  }

  // 查询用户
  async findUserByName(name) {
    const statement = "SELECT * FROM `user` WHERE name = ?";
    const [values] = await connection.execute(statement, [name]);
    return values;
  }

  async updateUserAvatar(avatarUrl, userId) {
    const statement = "UPDATE user SET avatar_url = ? WHERE id = ?";
    const [result] = await connection.execute(statement, [avatarUrl, userId]);
    return result;
  }
}

module.exports = new UserService();
